package com.example.day13;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.TextView;

public class Main3Activity extends AppCompatActivity {
    /**
     * 发送自定义播放器通知，并设置通知按钮广播事件
     *
     * @param savedInstanceState
     */
    TextView send;
    musicReceiver musicReceiver;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what==100)//得到这个消息标记开始处理消息
//                //处理消息
                send.setText(msg.obj.toString());
        }

    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        musicReceiver= new musicReceiver(handler);
        send = findViewById(R.id.text1);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customer_notification();
            }
        });

        IntentFilter intentFilter2=new IntentFilter();
////        //TODO 2：调频：
        intentFilter2.addAction("com.guwei.music");
        registerReceiver(musicReceiver,intentFilter2);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(musicReceiver);//解除注册
    }

    private void customer_notification() {
        //TODO 1：获得通知管理者：发送通知 取消通知
        NotificationManager manager = (NotificationManager)
                getSystemService(Context.NOTIFICATION_SERVICE);
        //TODO 2:创建构造者
        Notification.Builder builder = new Notification.Builder(this);
        //TODO 3:设置属性   setSamllIcon该属性必须设置
        builder.setSmallIcon(R.mipmap.ic_launcher);//设置小图标
        builder.setContentTitle("我是标题");
//        builder.setContentText("我是内容");
        //TODO :设置自定义布局：
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.play_item);


        //设置按钮
//        remoteViews.setTextViewText(R.id.button1, "没了");
        //设置点击的事件
        Intent buttonIntent = new Intent("com.guwei.music");
        buttonIntent.putExtra("music", "last");
        PendingIntent intent_paly = PendingIntent.getBroadcast(this, 10, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.button1, intent_paly);



        Intent buttonIntent2 = new Intent("com.guwei.music");
        buttonIntent2.putExtra("music", "play");
        PendingIntent intent_paly2 = PendingIntent.getBroadcast(this, 11, buttonIntent2, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.button2, intent_paly2);

        Intent buttonIntent3 = new Intent("com.guwei.music");
        buttonIntent3.putExtra("music", "next");
        PendingIntent intent_paly3 = PendingIntent.getBroadcast(this, 12, buttonIntent3, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.button3, intent_paly3);


        builder.setContent(remoteViews);
        //TODO 4:发送通知
        //参数一 id 通知的id   参数二 通知对象
        manager.notify(2, builder.build());

    }
}
